//
//  SetPhoneLockRecordViewController.m
//  IntelligentHousehold
//
//  Created by Chen on 2017/6/25.
//  Copyright © 2017年 Cassie. All rights reserved.
//

#import "SetPhoneLockRecordViewController.h"
#import "SetPhoneLockRecordTableViewCell.h"

#import "SetPhoneLockModle.h"



@interface SetPhoneLockRecordViewController ()<UITableViewDelegate,UITableViewDataSource>
{
    NSInteger page;
    NSString *msgTitle;//发送短信内容
}
/**  */
@property (nonatomic , strong) UITableView *tableView;
/**  */
@property (nonatomic , strong) NSMutableArray *dataArray;
@end

@implementation SetPhoneLockRecordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.view.backgroundColor = BaseColor;
    self.navigationItem.title = @"查询电话开锁记录";
    
    self.dataArray = [NSMutableArray array];
    [self createTableView];
    [self addNavUI];
    
    
    [self refreshAction];
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(refreshAction)];
    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(footRe)];
    
}

- (void)refreshAction{
    page = 1;
    [self getList];
}

-(void)footRe{
    
    page++;
    [self getList];
}
-(void)addNavUI{
    UIBarButtonItem *searchItem = [[UIBarButtonItem alloc] initWithTitle:@"查询" style:UIBarButtonItemStyleDone target:self action:@selector(searchAction)];//initWithImage:[UIImage imageNamed:@"sy_sz"] style:(UIBarButtonItemStyleDone) target:self action:@selector(setItemAction)];
    searchItem.tintColor = [UIColor whiteColor];
    self.navigationItem.rightBarButtonItem = searchItem;
}
-(void)searchAction{
    [self refreshAction];
}

- (void)createTableView{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kUIScreenWidth, kUIScreenHeight-64) style:(UITableViewStyleGrouped)];
    self.tableView.backgroundColor = BaseColor;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.rowHeight = 80;
    self.tableView.bounces = YES;
    self.tableView.showsVerticalScrollIndicator = NO;
    self.tableView.showsHorizontalScrollIndicator = NO;
    [self.tableView registerNib:[UINib nibWithNibName:@"SetPhoneLockRecordTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell"];
    
    
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:self.tableView];
    
    self.tableView = self.tableView;
}



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    SetPhoneLockRecordTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    SetPhoneLockModle *modle = self.dataArray[indexPath.row];
    
    NSString *telStr = modle.mtell;
    
    //base64解密
    NSData *nsdataFromBase64String = [[NSData alloc] initWithBase64EncodedString:telStr options:0];
    NSString *base64Decoded = [[NSString alloc] initWithData:nsdataFromBase64String encoding:NSUTF8StringEncoding];
    
    cell.xuhaoLabel.text = [NSString stringWithFormat:@"序号: %@",modle.xuliehao];
    cell.phoneLabel.text = base64Decoded;
    cell.dateLabel.text = modle.time;
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 10;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kUIScreenWidth, 10)];
    view.backgroundColor = BaseColor;
    return view;
}


-(void)getList{
    NSDictionary *saveDic = [SaveDataMethord readLoginData];
    
    NSString *url = [NSString stringWithFormat:KSUNLOCKCORD,RealmName,[NSString stringWithFormat:@"%ld",page]];

    NSMutableDictionary *parameDic = [NSMutableDictionary dictionary];
    [parameDic setObject:saveDic[@"user_id"] forKey:@"user_id"];
    [parameDic setObject:saveDic[@"token"] forKey:@"token"];
    [parameDic setObject:@"queryhistory" forKey:@"command"];
    [parameDic setObject:saveDic[@"locknumber"] forKey:@"locknumber"];

    
    [HttpRequest postWithURLString:url parameters:parameDic success:^(id responseObject) {
        if (page == 1) {
            [self.dataArray removeAllObjects];
        }
        
        NSMutableArray *tempArr = [SetPhoneLockModle mj_objectArrayWithKeyValuesArray:[responseObject objectForKey:@"data"]];
        if (tempArr.count) {
            [self.dataArray addObjectsFromArray:tempArr];
        }
        
        
        [self.tableView reloadData];
        
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
    } failure:^(NSError *error) {
        
    }];
    
    
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
